<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>医疗管家</title>
    <meta name="description" content="医疗管家">
    <meta name="keywords" content="医疗管家">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<style>
    .custom-tree-node {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        padding-right: 8px;
    }
</style>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="请输入关键字进行过滤" v-model="name" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="findPage()" type="success" class="dalfBut">查询</el-button>
            </div>

            <!--树形控件-->
            <div class="custom-tree-container">
                <div class="block">
                    <el-tree :data="data" show-checkbox node-key="id" default-expand-all :expand-on-click-node="false">
                    <span class="custom-tree-node" slot-scope="{ node, data }">
                        <span>{{node.label}}</span>
                    <span>
                        <el-button type="primary" size="mini" @click="() => append(data)">修改</el-button>
                        <el-button type="danger" size="mini" @click="() => remove(node, data)">删除</el-button>
                    </span>
                    </span>
                     </el-tree>
                </div>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <!-- 被校验的表单必须有这个属性 ref -->
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父菜单">
                                    <el-select v-model="formData.fumenu">
                                        <el-option label="男" value="男"></el-option>
                                        <el-option label="女" value="女"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="URL" prop="url">
                                    <el-input v-model="formData.url"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="path" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="优先权" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="icon" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明" prop="remark">
                                    <el-input v-model="formData.remark"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父菜单">
                                    <el-select v-model="formData.fumenu">
                                        <el-option label="男" value="男"></el-option>
                                        <el-option label="女" value="女"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="URL" prop="url">
                                    <el-input v-model="formData.url"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="path" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="优先权" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="icon" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明" prop="remark">
                                    <el-input v-model="formData.remark"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            data: [{
                id: 1,
                label: '一级 1',
                children: [{
                    id: 4,
                    label: '二级 1-1',
                }]
            }],
            name:'',
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                username: [{ required: true, message: '姓名为必填项', trigger: 'blur' }],
                password: [{ required: true, message: '密码为必填项', trigger: 'blur' }]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.listPage();
        },
        methods: {
            append(data) {
                const newChild = { id: id++, label: 'testtest', children: [] };
                if (!data.children) {
                    this.$set(data, 'children', []);
                }
                data.children.push(newChild);
            },

            remove(node, data) {
                const parent = node.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(d => d.id === data.id);
                children.splice(index, 1);
            },

            listPage(){
                axios.get("http://localhost:9002/menu/list").then(response=>{
                    console.log(response);
                    this.dataList = response.data.data;
                });
            },
            myMessage(response){
                if (response.data.flag){
                    this.$message.success(response.data.message);
                    //刷新当前数据
                    this.listPage();
                    //重置表单
                    this.resetForm();
                }else{
                    this.$message.error(response.data.message);
                }
            },
            //编辑检查项
            handleEdit() {
                this.$refs['dataEditForm'].validate((valid)=>{
                    if (valid){
                        axios.put("http://localhost:9002/menu/",this.formData).then(response=>{
                            this.myMessage(response);
                        })
                    }else{
                        this.$message.error("校验未通过");
                        return false;
                    }
                })
            },
            //添加检查项
            handleAdd () {
               this.$refs['dataAddForm'].validate((valid)=>{
                   if (valid){
                       axios.post("http://localhost:9002/menu/",this.formData).then(response=>{
                           this.myMessage(response);
                       })
                   }else{
                       this.$message.error("校验未通过");
                       return false;
                   }
               })
            },
            //分页查询
            findPage() {
               this.listPage();
            },
            //重置表单
            resetForm() {
                //关闭弹框
                this.dialogFormVisible = false;
                this.dialogFormVisible4Edit = false;
                //清空表单
                this.formData = {};
            },
            //弹出添加窗口
            handleCreate() {
               this.formData = {};
               this.dialogFormVisible = true;
            },
            //弹出编辑窗口
            handleUpdate(row) {
               axios.get(`http://localhost:9002/menu/${row.id}`).then(response=>{
                   if (response.data.flag){
                       this.dialogFormVisible4Edit = true;
                       this.formData = response.data.data;
                   }else{
                       this.$message.error(response.data.message);
                   }
               })
            },
            // 删除检查项
            handleDelete(row) {
                let id = row.id;
                this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete(`http://localhost:9002/menu/${id}`).then(response=>{
                        this.myMessage(response);
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>